package org.example.main;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WangShengJie
 * @Date: 2025/5/14
 */
@Slf4j
public class SplitDemo {
    public static void main(String[] args) {

        String addr = "110000@110100@110108@海淀南路海淀南路(亿方大厦)";
//        int count = 0;
//        for (int i = 0; i <= addr.length()-1; i++) {
//            if ("@".equals(addr.substring(i, i + 1))) {
//                count++;
//            }
//        }
//        System.out.println(count);
//        String[] addrArry =  new String[count];
//        for (int i = 0; i < addrArry.length; i++) {
//            addrArry[i] = addr.substring(0, addr.indexOf("@"));
//            addr = addr.replaceFirst(addrArry[i] + "@", "");
//        }
//        System.out.println(Arrays.toString(addrArry));
        Map<String, String> map = revertAddress(addr);
        System.out.println(map);

    }

    private static Map<String,String> revertAddress(String str) {
        log.info("地址转换入参：{}", str);
        Map<String, String> map = new HashMap<>();
        try {
            String province = "";
            String city = "";
            String country = "";
            String town = "";
            String detail = "";
            int i = 1;
            while (!(str.indexOf("@") < 0)) {
                String address = str.substring(0, str.indexOf("@"));
                str = str.replaceFirst(address + "@", "");
                System.out.println(str);
                if (i == 1) {
                    province = address;
                    map.put("province", province);
                } else if (i == 2) {
                    city = address;
                    map.put("city", city);
                } else if (i == 3) {
                    country = address;
                    map.put("country", country);
                } else if (i == 4) {
                    town = address;
                    map.put("town", town);
                    break;
                }
                i++;
            }
            map.put("detail", str);
            log.info("地址转换结果：{}", map);
        } catch (Exception e) {
            log.error("地址转换出错~~~入参：{}", str, e);
        }
        return map;
    }
}
